import requests
import re
import subprocess


def get_index_m3u8():
    url = "https://www.qingjie123.com/nmuy/bairimengxiangjia/1-1.html"

    headers = {
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "accept-encoding": "gzip, deflate, br, zstd",
        "accept-language": "zh-CN,zh;q=0.9",
        "cache-control": "no-cache",
        "cookie": "zanpian_playlog=think%3A%7B%22id_16840%22%3A%7B%22log_vid%22%3A%2216840%22%2C%22log_sid%22%3A%221%22%2C%22log_pid%22%3A%221%22%2C%22log_urlname%22%3A%22HD%25E4%25B8%25AD%25E5%25AD%2597%25E7%2589%2588%22%2C%22log_maxnum%22%3A%221%22%2C%22log_addtime%22%3A%221755677377%22%7D%7D; PHPSESSID=6vk3oer8b43pm48q9s1dm06vnk; HWTOKEN=74960833f348807a81ef2856de2cb8af; Hm_tf_1ohqyrlmmfx=1755677375; Hm_lvt_1ohqyrlmmfx=1755677375; Hm_lpvt_1ohqyrlmmfx=1755677375; HWIDHASH=5f4577ce931caafc5ef5106a9ba97a7d; HWPID=k6bMTadC1Fhogage80mc18ZxTTNCa-JV08wd37fhMDk",
        "pragma": "no-cache",
        "sec-ch-ua": '"Not;A=Brand";v="99", "Google Chrome";v="139", "Chromium";v="139"',
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": '"Windows"',
        "sec-fetch-dest": "document",
        "sec-fetch-mode": "navigate",
        "sec-fetch-site": "none",
        "sec-fetch-user": "?1",
        "upgrade-insecure-requests": "1",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36",
    }

    resp = requests.get(url, headers=headers)

    # 用正则匹配 index.m3u8 字段
    m = re.search(r'"url":"(.*?)"', resp.text)
    if m:
        url = m.group(1)
        # 转义反斜杠
        indexm3u8 = url.replace("\\/", "/")
        return indexm3u8


def get_mixed_m3u8(url):
    headers = {
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate, br, zstd",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Cache-Control": "no-cache",
        "Origin": "https://www.qingjie123.com",
        "Pragma": "no-cache",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "cross-site",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36"
    }
    resp = requests.get(url, headers=headers)
    lines = resp.text.splitlines()
    return "https://cdn5.yzzy-online.com/20220605/21944_d5dac83f/" + \
        list([line for line in lines if line and not line.startswith("#")])[0]


def parse_mixed_m3u8(url):
    headers = {
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate, br, zstd",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Cache-Control": "no-cache",
        "Origin": "https://www.qingjie123.com",
        "Pragma": "no-cache",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "cross-site",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36"
    }
    resp = requests.get(url, headers=headers)
    # print(resp.text)


indexm3u8 = get_index_m3u8()
mixed_m3u8 = get_mixed_m3u8(indexm3u8)
# 调用 ffplay 播放
subprocess.run([
    "ffplay",
    "-i", mixed_m3u8,
    "-autoexit",  # 播放完自动退出
    "-stats",  # 显示进度和流信息
    "-x", "1280",  # 窗口宽度
    "-y", "720",  # 窗口高度
    "-analyzeduration", "1000000",  # 加快流分析速度
    "-fflags", "nobuffer",  # 减少延迟
    "-infbuf"  # 无限缓冲，保证流畅播放
])
